
//
//  LDWheel.m
//  Gift
//
//  Created by 李迪 on 15/11/9.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "LDWheel.h"
#import "BaseHeader.h"

@interface LDWheel ()<UIScrollViewDelegate>

@property (nonatomic, retain)UIScrollView *LBScrollView;
@property (nonatomic, retain)UIImageView *LBimageView;
@property (nonatomic, retain)NSMutableArray *imageArray;
@property (nonatomic, retain)NSTimer *timer;

@end


@implementation LDWheel

-(void)dealloc{
    
    self.LBScrollView.delegate = nil;
    [self.LBimageView release];
    [self.imageArray release];
    [self.timer release];
    [super dealloc];
}



-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self createScrollView];
    }
    return self;
}

#pragma mark - 创建录播图 并且从网络获取数据
-(void)createScrollView
{
    [PFAFNetworking getUrl:@"http://api.liwushuo.com/v2/banners?channel=iOS" body:nil response:PFJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        [self LBView];
        self.imageArray = [[NSMutableArray array]init];
        for (NSDictionary *dic in [[resposeObject objectForKey:@"data"]objectForKey:@"banners"]) {
        [self.imageArray addObject:[dic objectForKey:@"image_url"]];
    }
    [self.LBScrollView setContentSize:CGSizeMake((self.imageArray.count + 2) * self.frame.size.width, 0)];
    for (int i = 0; i < self.imageArray.count ; i++) {
        
    self.LBimageView = [[UIImageView alloc] init];
        [self.LBimageView sd_setImageWithURL:[NSURL URLWithString:self.imageArray[i]]placeholderImage:[UIImage imageNamed:@"leimi"]];
    [self.LBimageView setFrame:CGRectMake((i + 1) * self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
    [self.LBScrollView addSubview:self.LBimageView];
    [self.LBimageView release];

    }
    // 第一个 和最后一个  用于循环播出
    UIImageView *imFist = [[UIImageView alloc] init];
    [imFist sd_setImageWithURL:[NSURL URLWithString:[self.imageArray lastObject]]];
    [imFist setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [self.LBScrollView addSubview:imFist];
    
    UIImageView *imLost = [[UIImageView alloc] init];
    [imLost sd_setImageWithURL:[NSURL URLWithString:[self.imageArray firstObject]]];
    [imLost setFrame:CGRectMake(self.frame.size.width * (self.imageArray.count + 1), 0, self.frame.size.width, self.frame.size.height)];
    [self.LBScrollView addSubview:imLost];
    [self.LBScrollView setContentOffset:CGPointMake(self.frame.size.width, 0)];
    
    [imFist release];
    [imLost release];
    [_LBimageView release];
    [_LBScrollView release];
    
    // 设置定时器 自动滚动
        
   self.timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(Timer) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
   
}

#pragma mark - 创建ScrollView
-(void)LBView
{
    _LBScrollView = [[UIScrollView alloc] init];
    _LBScrollView.delegate = self;
    [_LBScrollView setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [_LBScrollView setPagingEnabled:YES];
    [self addSubview:_LBScrollView];
    
    [self.LBimageView release];
}
#pragma mark - 自动滚动方法实现
-(void)Timer
{
    
    [_LBScrollView setContentOffset:CGPointMake(_LBScrollView.contentOffset.x + self.frame.size.width, 0) animated:YES];
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if (scrollView.contentOffset.x == 0) {
        [scrollView setContentOffset:CGPointMake(self.frame.size.width * self.imageArray.count, 0) animated:NO];
    }
    if (scrollView.contentOffset.x == self.frame.size.width * (_imageArray.count + 1)) {
        [scrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
    }

}








/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
